package com.example.actearly_revised;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;


import android.content.Context;

public class Creating_Node_List {
	Context newContext;
	int count;
	public static HashMap<Integer, Integer> milestoneYearMap = new HashMap<Integer, Integer>();
	static Map<Integer, LinkedList<Question_Node>> milestone_map = new HashMap<Integer, LinkedList<Question_Node>>();
	SQLogic conn;
	public Creating_Node_List(int milestone, Context con){
		this.newContext = con;
		int[] ageID = new int[] {
				2,4,6,9,12,18,24,36,48,60
		};
		milestone_map.put(milestone, new LinkedList<Question_Node>());
		for(int i = 0; i < ageID.length; i++){
			milestoneYearMap.put(ageID[i], 0);
		}
		//System.out.println(milestoneYearMap);
	}
	
	public void increment(int milestone){
		int temp = milestoneYearMap.get(milestone);
		milestoneYearMap.remove(milestone);
		temp++;
		milestoneYearMap.put(milestone, temp);
	}

	public void decrement(int milestone){
		int temp = milestoneYearMap.get(milestone);
		milestoneYearMap.remove(milestone);
		temp--;
		milestoneYearMap.put(milestone, temp);
	}
	
	public int getCountOfMilestone(int milestone){
		//System.out.println(milestone);
		int temp = milestoneYearMap.get(milestone);
		//System.out.println(">>>>>>>>>>>" + temp);
		return temp;
	}
	
	public void setCount(int milestone, int count){
		milestoneYearMap.remove(milestone);
	
		milestoneYearMap.put(milestone, count);
	}
	
	public void add(int milestone, int id){
		milestone_map.put(milestone, new LinkedList<Question_Node>());
		conn = new SQLogic(newContext);
		Change_of_Answer_Database.setConnection(newContext);
		conn.open();
		//QuestionsObject qd =  conn.RetrieveAllQuestions(milestone ,0);
		ArrayList<QuestionAnswerGroup> answerGroup =  conn.GetQuestionAnswerGroup(id, milestone);

		//System.out.println(qd.newMap.keySet());
		//Iterator<String> newIterator = qd.getMap().keySet().iterator();
		QuestionsObject qd = new QuestionsObject();

		for(int i = 0; i < answerGroup.size(); i++){
			//System.out.println("?????????????????"+answerGroup.get(i).getQuestion());
			qd.getMap().put(""+answerGroup.get(i).getQuestionId(), new Node(milestone, answerGroup.get(i).getQuestion(), 0,"0", answerGroup.get(i).getQuestionId()));
			qd.getMap().get(""+answerGroup.get(i).getQuestionId()).setAnswer(answerGroup.get(i).getAnswer());
		}
		
		Collection<Node> c = qd.getMap().values();
		Iterator<Node> newIterator = c.iterator();
		int countMilestone = -1;
		while(newIterator.hasNext()){
			//Iterator<String> temp = newIterator;
			Node temp1 = newIterator.next();
			//Question_Node tempNode = new Question_Node(qd.getMap().get(temp1).Question, 0, qd.getMap().get(temp1).q_id);
			int newTemp = 0;
			Boolean holdAnswer = false;
			if(temp1.answer == null){
				newTemp = 0;
				//System.out.println("<><><><><><><>"+countMilestone);

			}
			else{
				countMilestone++;
				//System.out.println("<><><><><><><>"+countMilestone);
				setCount(milestone, countMilestone);
				//System.out.println(temp1.answer+">>>>>>>>>>");
				if(temp1.answer.equals("yes")){
				newTemp = 1;
				holdAnswer = true;
				}
				else if(temp1.answer.equals("no")){
					newTemp =0;
					holdAnswer = true;
				}
				else{
					newTemp =2;
					holdAnswer = true;
				}
				//System.out.println("LLLLLLLLLLLLLLLLLLLLLKKKKKKK"+newTemp);
			}
			Question_Node tempNode = new Question_Node((temp1).Question, newTemp, (temp1).q_id);
			if(holdAnswer == true){
			tempNode.flag_answered=true;
			}
			//System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>"+tempNode.question +"<<<<<<<<<<<<<<<<<");
			if(!milestone_map.get(milestone).contains(tempNode)){
			milestone_map.get(milestone).add(tempNode);
			
			//System.out.println(milestone_map.get(milestone).get(count).question);
			count++;
			}
			
		}
		//Collections.sort((milestone_map.get(milestone).get(count), new Collator<Integer>());
		LinkedList<Question_Node> tempList = (milestone_map.get(milestone));
		Collections.sort(tempList, new Comparator<Question_Node>() {

		   
			@Override
			public int compare(Question_Node lhs, Question_Node rhs) {
				// TODO Auto-generated method stub
				Integer o1 = new Integer(lhs.q_id);
				Integer o2 = new Integer(rhs.q_id);

				return o1.compareTo(o2);
			}
		});
		
		
		conn.close();
	}

	public static void change(int milestone, String question, int answer, Context context, int profile_number,boolean bool){
		System.out.println(question);
		
		int temp = 0;
		//System.out.println("????????????" + answer);
		//System.out.println(milestone_map.get(milestone));
		for(int j = 0; j < milestone_map.get(milestone).size(); j++){
			if(milestone_map.get(milestone).get(j).getQuestion().equals(question)){
				temp = j;
			}
		}
		if(bool == false){
			return;
		}
		//System.out.println(temp);

			milestone_map.get(milestone).get(temp).changeAnswer(answer, context, profile_number);
		
		//milestone_map.get(milestone).get(temp).changeAnswer(answer);
		//milestone_map.get(milestone).get(temp).flag_answered = true;
	}
	
	public static int gettingLastEditedAnswer(int milestone){
		LinkedList<Question_Node> tempList = milestone_map.get(milestone);
		//System.out.println(tempList);
		for(int i =0; i < tempList.size(); i++){
			//System.out.println(tempList.get(i).getAnswer());
			if(tempList.get(i).flag_answered == false){
				return i;
			}
		}
		return tempList.size()-1;
	}


}
